/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			userfunc.cc
  \date			Dec 2011
  \author		TNick

  \brief		Contains the implementation for UserFunc class


*//*

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<pgscript.h>
#include	<pgscript_internal.h>

#include	<session.h>
#include	<module.h>

#include	"userfunc.h"


/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

using namespace PgScr;

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

extern findVariableStruct		uf_findVarStr;
extern findVariableName			uf_findVarName;
extern findFunctionStruct		uf_findFncStr;
extern findFunctionName			uf_findFncName;

/*  DATA    ================================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */


/* ------------------------------------------------------------------------- */
void			UserFunc::construct			( Module * mdl )
{
	flg_ = FL_NONE;

	/** @todo mdl->appendVar( this ); */
}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
UserFunc::UserFunc	( void )
{
	construct( Session::defModule() );
}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
UserFunc::UserFunc		( QString f_name, Module * mdl )
{
	if ( mdl == NULL )
	{
		mdl = Session::defModule();
	}
	construct( mdl );
	name_ = f_name;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
UserFunc::UserFunc		( QString f_name, Session * trg_ss )
{
	if ( trg_ss == NULL )
	{
		construct( Session::defModule() );
	}
	else
	{
		construct( trg_ss->sessionDefModule() );
	}
	name_ = f_name;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
UserFunc::UserFunc		(
		QString f_name, UserFunc::Flags flgs, Module * mdl )
{

	if ( mdl == NULL )
	{
		mdl = Session::defModule();
	}
	construct( mdl );

	name_ = f_name;
	flg_ = flgs;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
UserFunc::UserFunc		(
		Session *trg_ss, QString f_name,
		Flags flgs )
{

	if ( trg_ss == NULL )
	{
		construct( Session::defModule() );
	}
	else
	{
		construct( trg_ss->sessionDefModule() );
	}

	name_ = f_name;
	flg_ = flgs;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
UserFunc::~UserFunc	( void )
{
	/* stub */
}
/* ========================================================================= */



#ifdef	PGSCRIPT_TESTS
/* ------------------------------------------------------------------------- */
void				PgScr::Func_Tests					(
		int & performed, int & succeded )
{
	QString		Outcm;
	performed = 0;
	succeded = 0;



	Session * new_s = createSession();
	Module * new_m = new Module( "func_test_module" );

	int test_sz = 1000;
	int i;
	int j = 0;
	bool b_ok =  true;
	qDebug() << "Now testing with a number of " << test_sz << " functions";
	UserFunc ** mdl_l = (UserFunc **)malloc( sizeof(UserFunc *) * test_sz );
	UserFunc ** mdl_itr = mdl_l;

	for ( i = 0; i < test_sz; i++ )
	{

		switch ( j ){
		case	0:	{/* nr checked for !FL_PREDEF below */
			mdl_itr[i] = new UserFunc( );

			break; }
		case	1:	{/* nr checked for !FL_PREDEF below */
			mdl_itr[i] = new UserFunc( QString::number( i ) );
			break; }
		case	2:	{/* nr checked for !FL_PREDEF below */
			mdl_itr[i] = new UserFunc( QString::number( i ), new_m );
			break; }
		case	3:	{/* nr checked for !FL_PREDEF below */
			mdl_itr[i] = new UserFunc( QString::number( i ), new_s );
			break; }
		case	4:	{/* nr checked for FL_PREDEF below */
			mdl_itr[i] = new UserFunc( QString::number( i ),
									  UserFunc::FL_PREDEF);
			break; }
		case	5:	{/* nr checked for FL_PREDEF below */
			/* numbers starting with this must have callbacks */
			mdl_itr[i] = new UserFunc( QString::number( i ),
									  UserFunc::FL_PREDEF);
			break; }
		case	6:	{/* nr checked for FL_PREDEF below */
			/* nr checked for validator below */
			mdl_itr[i] = new UserFunc( QString::number( i ),
									  UserFunc::FL_PREDEF,
									  new_m);
			break; }
		case	7:	{/* nr checked for FL_PREDEF below */
			mdl_itr[i] = new UserFunc( new_s, QString::number( i ),
									  UserFunc::FL_PREDEF);
			break; }
		case	8:	{/* nr checked for FL_PREDEF below */
			/* nr checked for validator below */
			mdl_itr[i] = new UserFunc( new_s, QString::number( i ),
									  UserFunc::FL_PREDEF);
			break; }
		default:	break;
		}
		j++; if ( j >= 8 ) j = 0;

	}

	for ( i = 0; i < test_sz; i++ )
	{
		if ( j == 0 )
		{
			mdl_itr[i]->setName( QString::number( i ) );
		}
		j++; if ( j >= 8 ) j = 0;
	}

	for ( i = 0; i < test_sz; i++ )
	{
		if ( mdl_itr[i]->name() != QString::number( i ) )
		{
			b_ok = false;
			break;
		}
	}
	if ( b_ok )	TEST_OK
			else	TEST_FAIL
			qDebug() << " - checking name() and setName()..." << Outcm;
	performed++; b_ok = true;


	for ( i = 0; i < test_sz; i++ )
	{
		if ( j < 4 )
		{
			if ( mdl_itr[i]->isPredef() )
			{
				b_ok = false;
				break;
			}
		}
		else
		{
			if ( ! mdl_itr[i]->isPredef() )
			{
				b_ok = false;
				break;
			}
		}
		j++; if ( j >= 8 ) j = 0;
	}
	if ( b_ok )	TEST_OK
			else	TEST_FAIL
			qDebug() << " - checking read only and predefined..." << Outcm;
	performed++; b_ok = true;


	for ( i = 0; i < test_sz; i++ )
	{
		mdl_itr[i]->makePermanent();
	}
	for ( i = 0; i < test_sz; i++ )
	{
		if ( ! mdl_itr[i]->isPermanent() )
		{
			b_ok = false;
			break;
		}
	}
	if ( b_ok )
	{
		for ( i = 0; i < test_sz; i++ )
		{
			mdl_itr[i]->makeVolatile();
		}
		for ( i = 0; i < test_sz; i++ )
		{
			if ( mdl_itr[i]->isPermanent() && ( mdl_itr[i]->isPredef() == false ) )
			{
				b_ok = false;
				break;
			}
		}
	}
	if ( b_ok )	TEST_OK
			else	TEST_FAIL
			qDebug() << " - checking permanent flag..." << Outcm;
	performed++; b_ok = true;




}
/* ========================================================================= */
#endif

/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
